Ext.require([
    'Ext.util.History'
]);

Ext.define('MySharedData', {
    singleton:true,
    vak:null
});

Ext.define('ProjectAON.controller.Startup', {
    extend:'Ext.app.Controller',

    requires:[
        'ProjectAON.ux.ActivityMonitor',
        'ProjectAON.util.Hash'
    ],

    stores:[
        'Login',
        'Leerling',
        'BeheerLeraar',
        'CompetentiesLeerling',
        'CompetentiesVak',
        'CompetentiesLeerlingen',
        'ControleScore',
        'Deelcompetenties',
        'DeelcompetentiesPerVak',
        'Hoofdcompetenties',
        'HoofdcompetentiesPerVak',
        'Klas',
        'PeerEvaluatiesLeerlingen',
        'PeerEvaluatiesKlas',
        'Portfolio',
        'Subcompetenties',
        'SubcompetentiesPerVak',
        'BeschikbarePeerEvaluatiesKlas',
        'ToevoegenCompetenties',
        'Vak',
        'ZelfEvaluatiesKlas',
        'ZelfEvaluatiesLeerling',
        'Indicatoren',
        'LeerlingVanVak'
    ],

    models:[
        'Login',
        'Leerling',
        'BeheerLeraar',
        'CompetentiesLeerling',
        'CompetentiesVak',
        'CompetentiesLeerlingen',
        'ControleScore',
        'Deelcompetenties',
        'DeelcompetentiesPerVak',
        'Hoofdcompetenties',
        'HoofdcompetentiesPerVak',
        'Klas',
        'PeerEvaluatiesLeerlingen',
        'PeerEvaluatiesKlas',
        'Portfolio',
        'Subcompetenties',
        'SubcompetentiesPerVak',
        'BeschikbarePeerEvaluatiesKlas',
        'ToevoegenCompetenties',
        'Vak',
        'ZelfEvaluatiesKlas',
        'ZelfEvaluatiesLeerling',
        'Indicatoren',
        'LeerlingVanVak'
    ],

    views:[
        'home.Scherm1',
        'home.Scherm25',
        'home.Scherm36',
        'home.Login',
        'competentiesBeheren.Scherm26',
        'evaluatieBekijken.Scherm2',
        'evaluatieBekijken.Scherm3',
        'evaluatieBekijken.Scherm4',
        'evaluatieBekijken.Scherm5',
        'evaluatieBekijken.Scherm6',
        'evaluatieBekijken.Scherm7',
        'evaluatieIngeven.Scherm8',
        'evaluatieIngeven.Scherm9',
        'evaluatieIngeven.Scherm10',
        'evaluatiesBeheren.Scherm33',
        'evaluatiesBeheren.Scherm34',
        'evaluatiesBeheren.Scherm35',
        'leerlingenBeheren.Scherm31',
        'leerlingenBeheren.Scherm32',
        'lerarenBeheren.Scherm29',
        'lerarenBeheren.Scherm30',
        'overzichtCompetenties.Scherm11',
        'overzichtCompetenties.Scherm12',
        'overzichtLeerlingenCompetenties.Scherm14',
        'overzichtLeerlingenCompetenties.Scherm15',
        'peerEvaluatie.Scherm38',
        'peerEvaluatie.Scherm39',
        'peerOpenzetten.Scherm16',
        'peerOpenzetten.Scherm18',
        'peerBekijken.Scherm19',
        'peerBekijken.Scherm20',
        'portfolio.Scherm37',
        'vakkenBeheren.Scherm27',
        'vakkenBeheren.Scherm28',
        'vakkenBeheren.Scherm45',
        'zelfOpenzetten.Scherm21',
        'zelfOpenzetten.Scherm22',
        'zelfEvaluatie.Scherm40',
        'zelfEvaluatie.Scherm41',
        'zelfEvaluatie.Scherm44',
        'zelfEvaluatie.Scherm46',
        'zelfBekijken.Scherm23',
        'zelfBekijken.Scherm24',
        'indicatorenBeheren.Scherm42',
        'indicatorenBeheren.Scherm43',
        'Viewport'
    ],

    init:function () {
        Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider'));
        Ext.History.init();
        this.control({
            'login window form button[action=login]':{
                click:this.login
            },
            'viewport':{
                beforerender:this.onBeforeRenderer
            },

            'scherm1 button[action=logout]':{
                click:this.logout
            },
            'scherm1 button[action=scherm2]':{
                click:this.scherm2
            },
            'scherm1 button[action=scherm8]':{
                click:this.scherm8
            },
            'scherm1 button[action=scherm11]':{
                click:this.scherm11
            },
            'scherm1 button[action=scherm14]':{
                click:this.scherm14
            },
            'scherm1 button[action=scherm16]':{
                click:this.scherm16
            },
            'scherm1 button[action=scherm19]':{
                click:this.scherm19
            },
            'scherm1 button[action=scherm21]':{
                click:this.scherm21
            },
            'scherm1 button[action=scherm23]':{
                click:this.scherm23
            },

            'scherm2 button[action=scherm3]':{
                click:this.scherm3
            },
            'scherm2 button[action=back]':{
                click:this.scherm1
            },

            'scherm3 button[action=back]':{
                click:this.scherm2
            },
            'scherm3 button[action=scherm4]':{
                click:this.scherm4
            },
            'scherm3 button[action=scherm6]':{
                click:this.scherm6
            },

            'scherm4 grid[action=scherm5]':{
                itemdblclick:this.scherm5
            },
            'scherm4 button[action=back]':{
                click:this.scherm3
            },

            'scherm5 button[action=back]':{
                click:this.scherm4
            },

            'scherm6 button[action=back]':{
                click:this.scherm3
            },
            'scherm6 grid[action=scherm7]':{
                itemdblclick:this.scherm7
            },

            'scherm7 button[action=back]':{
                click:this.scherm6
            },

            'scherm8 button[action=scherm9]':{
                click:this.scherm9
            },
            'scherm8 button[action=back]':{
                click:this.scherm1
            },

            'scherm9 grid[action=scherm10]':{
                itemdblclick:this.scherm10
            },
            'scherm9 button[action=back]':{
                click:this.scherm8
            },

            'scherm10 button[action=back]':{
                click:this.scherm9
            },

            'scherm11 button[action=scherm12]':{
                click:this.scherm12
            },
            'scherm11 button[action=back]':{
                click:this.scherm1
            },

            'scherm12 button[action=back]':{
                click:this.scherm11
            },

            'scherm14 button[action=back]':{
                click:this.scherm1
            },

            'scherm15 button[action=back]':{
                click:this.scherm14
            },

            'scherm16 button[action=scherm18]':{
                click:this.scherm18
            },
            'scherm16 button[action=back]':{
                click:this.scherm1
            },

            'scherm18 button[action=back]':{
                click:this.scherm16
            },

            'scherm19 grid[action=scherm20]':{
                itemdblclick:this.scherm20
            },
            'scherm19 button[action=back]':{
                click:this.scherm1
            },

            'scherm20 button[action=back]':{
                click:this.scherm19
            },

            'scherm21 button[action=scherm22]':{
                click:this.scherm22
            },
            'scherm21 button[action=back]':{
                click:this.scherm1
            },

            'scherm22 button[action=back]':{
                click:this.scherm21
            },

            'scherm23 grid[action=scherm24]':{
                itemdblclick:this.scherm24
            },
            'scherm23 button[action=back]':{
                click:this.scherm1
            },

            'scherm24 button[action=back]':{
                click:this.scherm23
            },

            'scherm25 button[action=logout]':{
                click:this.logout
            },
            'scherm25 button[action=scherm26]':{
                click:this.scherm26
            },
            'scherm25 button[action=scherm27]':{
                click:this.scherm27
            },
            'scherm25 button[action=scherm29]':{
                click:this.scherm29
            },
            'scherm25 button[action=scherm31]':{
                click:this.scherm31
            },
            'scherm25 button[action=scherm33]':{
                click:this.scherm33
            },
            'scherm25 button[action=scherm42]':{
                click:this.scherm42
            },

            'scherm26 button[action=back]':{
                click:this.scherm25
            },

            'scherm27 button[action=scherm28]':{
                click:this.scherm28
            },
            'scherm27 button[action=scherm45]':{
                click:this.scherm45
            },
            'scherm27 button[action=back]':{
                click:this.scherm25
            },

            'scherm28 button[action=back]':{
                click:this.scherm27
            },

            'scherm29 button[action=scherm30]':{
                click:this.scherm30
            },
            'scherm29 button[action=back]':{
                click:this.scherm25
            },

            'scherm30 button[action=back]':{
                click:this.scherm29
            },

            'scherm31 button[action=scherm32]':{
                click:this.scherm32
            },
            'scherm31 button[action=back]':{
                click:this.scherm25
            },

            'scherm32 button[action=back]':{
                click:this.scherm31
            },

            'scherm33 button[action=scherm34]':{
                click:this.scherm34
            },
            'scherm33 button[action=back]':{
                click:this.scherm25
            },

            'scherm34 button[action=scherm35]':{
                click:this.scherm35
            },
            'scherm34 button[action=back]':{
                click:this.scherm33
            },

            'scherm35 button[action=back]':{
                click:this.scherm34
            },

            'scherm36 button[action=logout]':{
                click:this.logout
            },
            'scherm36 button[action=scherm37]':{
                click:this.scherm37
            },
            'scherm36 button[action=scherm38]':{
                click:this.scherm38
            },

            'scherm36 button[action=scherm44]':{
                click:this.scherm44
            },

            'scherm37 button[action=back]':{
                click:this.scherm36
            },

            'scherm37 panel[action=openSub]':{
                beforeexpand:this.panelCompetentieScore
            },

            'scherm38 button[action=back]':{
                click:this.scherm36
            },

            'scherm39 button[action=back]':{
                click:this.scherm36
            },

            'scherm40 button[action=back]':{
                click:this.scherm44
            },

            'scherm41 button[action=back]':{
                click:this.scherm36
            },

            'scherm42 button[action=back]':{
                click:this.scherm25
            },
            'scherm42 button[action=scherm43]':{
                click:this.scherm43
            },

            'scherm43 button[action=back]':{
                click:this.scherm42
            },

            'scherm44 button[action=back]':{
                click:this.scherm36
            },
            'scherm44 button[action=scherm46]':{
                click:this.scherm46
            },
            'scherm44 button[action=scherm40]':{
                click:this.scherm40
            },

            'scherm45 button[action=back]':{
                click:this.scherm27
            },
            'scherm46 button[action=back]':{
                click:this.scherm44
            },
            'scherm46 button[action=scherm40]':{
                click:this.scherm40
            },
            //TODO Andere schermen toevoegen
            'scherm2 textfield':{
                change:{
                    fn:this.filterButtons,
                    buffer:200
                }
            }, 'scherm8 textfield':{
                change:{
                    fn:this.filterButtons,
                    buffer:200
                }
            }, 'scherm11 textfield':{
                change:{
                    fn:this.filterButtons,
                    buffer:200
                }
            }, 'scherm16 textfield':{
                change:{
                    fn:this.filterButtons,
                    buffer:200
                }
            }, 'scherm21 textfield':{
                change:{
                    fn:this.filterButtons,
                    buffer:200
                }
            }, 'scherm27 textfield':{
                change:{
                    fn:this.filterButtons,
                    buffer:200
                }
            }, 'scherm33 textfield':{
                change:{
                    fn:this.filterButtons,
                    buffer:200
                }
            }, 'scherm42 textfield':{
                change:{
                    fn:this.filterButtons,
                    buffer:200
                }
            }, 'scherm44 textfield':{
                change:{
                    fn:this.filterButtons,
                    buffer:200
                }
            }
        });
    },

    logout:function (button) {
        this.openView('login');
    },

    login:function (button) {
        var win = button.up('window'),
            form = win.down('form'),
            user = form.getValues().username.toString(),
            pw = ProjectAON.util.Hash.md5(form.getValues().password.toString()),
            me = this,
            usernameRegex = /^[a-zA-Z0-9]*$/,
            passwordRegex = /^[a-zA-Z0-9]*$/;

        if (user != "" && pw != "" && usernameRegex.test(user) && passwordRegex.test(pw)) {
            Ext.getBody().mask('Loading...', 'ext-el-mask');
            Ext.Ajax.request({
                url:'data/login.php',
                method:'POST',
                params:{
                    "user":user
                },
                success:function (response) {
                    var result = Ext.decode(response.responseText);
                    if (result != undefined && result.length == 1) {
                        var userx = result[0].username,
                            pwx = result[0].password,
                            right = result[0].rights;

                        if (user == userx && pw == pwx) {
                            ProjectAON.ux.ActivityMonitor.init({
                                verbose:false,
                                interval:30000,
                                maxInactive:900000
                            });
                            ProjectAON.ux.ActivityMonitor.start();
                            win.close();
                            if (right == 3) {
                                Ext.state.Manager.set('user', 'leerling')
                                me.openView('scherm36');
                            } else if (right == 2) {
                                Ext.state.Manager.set('user', 'leraar')
                                me.openView('scherm1');
                            } else if (right == 1) {
                                Ext.state.Manager.set('user', 'admin')
                                me.openView('scherm25');
                            }
                        } else {
                            Ext.MessageBox.alert('Login error', 'Ongeldige gebruikersnaam/paswoord combinatie.');
                        }
                    } else {
                        Ext.MessageBox.alert('Login error', 'Ongeldige gebruikersnaam.');
                    }
                },
                failure:function (response) {
                    console.error(response.responseText);
                    Ext.MessageBox.alert('Error', 'Kan geen connectie maken met de databank. Probeer later nog eens.');
                },
                callback:function () {
                    Ext.getBody().unmask();
                }
            });
        } else {
            Ext.MessageBox.alert('Login error', 'Gelieve een gebruiksnaam en wachtwoord in te geven.');
        }
    },

    scherm1:function (button) {
        this.openView('scherm1');
    },
    scherm2:function (button) {
        this.openView('scherm2');
    },
    scherm3:function (button) {
        this.openView('scherm3', button.text);
    },
    scherm4:function (button) {
        this.getLeerlingVanVakStore().getProxy().extraParams = {vak:MySharedData.vak};
        this.getLeerlingVanVakStore().load();
        this.openView('scherm4');
    },
    scherm5:function (button) {
        var selection = Ext.ComponentQuery.query('#gridPanelSub')[0].getSelectionModel().getSelection();
        this.openView('scherm5');
    },
    scherm6:function (button) {
        this.openView('scherm6');
    },
    scherm7:function (button) {
        this.openView('scherm7');
    },
    scherm8:function (button) {
        this.openView('scherm8');
    },
    scherm9:function (button) {
        this.openView('scherm9');
    },
    scherm10:function (button) {
        this.openView('scherm10');
    },
    scherm11:function (button) {
        this.openView('scherm11');
    },
    scherm12:function (button) {
        this.openView('scherm12');
    },
    scherm14:function (button) {
        this.openView('scherm14');
    },
    scherm16:function (button) {
        this.openView('scherm16');
    },
    scherm18:function (button) {
        this.openView('scherm18');
    },
    scherm19:function (button) {
        this.openView('scherm19');
    },
    scherm20:function (button) {
        this.openView('scherm20');
    },
    scherm21:function (button) {
        this.openView('scherm21');
    },
    scherm22:function (button) {
        this.openView('scherm22');
    },
    scherm23:function (button) {
        this.openView('scherm23');
    },
    scherm24:function (button) {
        this.openView('scherm24');
    },
    scherm25:function (button) {
        this.openView('scherm25');
    },
    scherm26:function (button) {
        this.openView('scherm26');
    },
    scherm27:function (button) {
        this.openView('scherm27');
    },
    scherm28:function (button) {
        this.openView('scherm28', button.text);
    },
    scherm29:function (button) {
        this.openView('scherm29');
    },
    scherm30:function (button) {
        this.openView('scherm30');
    },
    scherm31:function (button) {
        this.openView('scherm31');
    },
    scherm32:function (button) {
        this.openView('scherm32');
    },
    scherm33:function (button) {
        this.openView('scherm33');
        this.filterButtons();
    },
    scherm34:function (button) {
        this.openView('scherm34', button.text);
    },
    //TODO #
    scherm35:function (button) {
        var selection = Ext.ComponentQuery.query('#gridPanelSub')[0].getSelectionModel().getSelection();
        if (selection.length < 1) {
            selection = Ext.ComponentQuery.query('#gridPanelDeel')[0].getSelectionModel().getSelection();
            if (selection.length < 1) {
                selection = Ext.ComponentQuery.query('#gridPanelHoofd')[0].getSelectionModel().getSelection();
            }
        }
        this.getControleScoreStore().getProxy().extraParams = {competentie:selection[0].data.id, vak:MySharedData.vak};
        this.getControleScoreStore().load();
        this.openView('scherm35');
    },
    scherm36:function (button) {
        this.openView('scherm36');
    },
    scherm37:function (button) {
        this.openView('scherm37');
    },
    scherm38:function (button) {
        this.openView('scherm38');
    },
    scherm39:function (button) {
        this.openView('scherm39');
    },
    scherm40:function (button) {
        this.openView('scherm40', button.text);
    },
    scherm41:function (button) {
        this.openView('scherm41');
    },
    scherm42:function (button) {
        this.openView('scherm42');
    },
    scherm43:function (button) {
        this.openView('scherm43', button.text);
    },
    scherm44:function (button) {
        this.openView('scherm44');
    },
    scherm45:function (button) {
        this.openView('scherm45');
    },
    scherm46:function (button) {
        this.openView('scherm46', button.text);
    },

    openView:function (view, vak) {
        if (vak != undefined && vak != 'Terug') {
            MySharedData.vak = vak;
        }
        if (Ext.state.Manager.get('user') != undefined && view == 'login') {
            ProjectAON.ux.ActivityMonitor.stop();
        } else if (Ext.state.Manager.get('user') == undefined) {
            view = 'login';
        }
        var viewport = Ext.getCmp('view');
        viewport.removeAll(true);
        viewport.add({xtype:view, vak:vak});
        viewport.doLayout();
        Ext.History.add(view);
    },

    onBeforeRenderer:function () {
        Ext.History.on('change', function (token) {
            var toonScherm,
                gebruiker = Ext.state.Manager.get('user');
            if (token != 'login' && gebruiker != undefined) {
                var scherm = token.substring(6);
                if (gebruiker == 'leraar') {
                    if (scherm < 25) {
                        toonScherm = token;
                    } else {
                        toonScherm = 'scherm1';
                    }
                    ;
                } else if (gebruiker == 'leerling') {
                    if (scherm > 35) {
                        toonScherm = token;
                    } else {
                        toonScherm = 'scherm36';
                    }
                    ;
                } else if (gebruiker == 'admin') {
                    if (scherm > 24 && scherm < 36 || scherm > 41) {
                        toonScherm = token;
                    } else {
                        toonScherm = 'scherm25';
                    }
                    ;
                }
                ;
                var viewport = Ext.getCmp('view');
                viewport.removeAll(true);
                viewport.add({xtype:toonScherm, vak:MySharedData.vak});
                viewport.doLayout();
            }
            ;
        });
    },

    //TODO Code verbeteren?
    filterButtons:function () {
        var buttons = Ext.ComponentQuery.query('button');
        Ext.each(buttons, function (button, index) {
            if (button.text != 'Terug') {
                if (Ext.util.Format.lowercase(button.text).indexOf(
                    Ext.util.Format.lowercase(Ext.ComponentQuery.query('textfield[name=searchField]')[0].getValue())) < 0) {
                    if (button.isVisible()) {
                        button.hide();
                    }
                } else {
                    button.show();
                }
            }
        });
    },

    panelCompetentieScore:function(panel){
        var grid=Ext.getCmp(panel.id+'Grid');
        var parentGrid=panel.id.substring(panel.id.length-1);
        var vak=panel.ownerCt.ownerCt.title;
        grid.getStore().getProxy().extraParams={vak:vak,parent:parentGrid,beoordeelde:2,beoordelaar:2};
        grid.getStore().reload();
        grid.getView().refresh();
    }
});